package com.example.springboot.mapper;

/*
 * 功能：Mapper层即dao层，用SQL语句与数据库交互，返回给service层
 * 日期：2024-3-22 22:22
 * 作者：RabbitQ
 */

import com.example.springboot.entity.User;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper //注入spring容器
public interface UserMapper {

    //SQL语句中—— #{参数名} ——动态解析函数参数（对象）的属性值，例如下面user对象（JavaBean形式）中的属性值
    //新增
    //这个SQL语句可以优化，不用逐个输入
    /*@Insert("insert into `user` (username, password, name, phone, email, address, role, avatar) " +
            "values (#{username}, #{password}, #{name}, #{phone}, #{email}, #{address}, #{role}, #{avatar} )")
    void insert(User user);*/

    //注册界面——新增用户，只有用户名和密码
    @Insert("insert into `user` (username, password) values" +
            " (#{username}, #{password} )")
    void insert(User user);

    //更新
    @Update("update `user` set username = #{username}, password = #{password}, password = #{password}, " +
            "name = #{name}, phone = #{phone}, email = #{email}, address = #{address}, avatar = #{avatar} " +
            "where id=#{id}")
    void updateUser(User user);

    //删除——根据id单个进行删除
    @Delete("delete from `user` where id=#{id}")
    void deleteUser(Integer id);

    //查询——全部用户数据
    @Select("select * from `user` order by id DESC")
    List<User> selectAll();

    //查询——根据username和name查询
    @Select("select * from `user` where username=#{username}  and name=#{name}  order by id DESC")
    List<User> select_By_name_username(String username, String name);

    //查询——根据username查询
    @Select("select * from `user` where username=#{username} order by id DESC")
    User selectByUsername(String username);

    //还需要完善——数据库中userId是Interger类型的，但是函数的参数是String类型的
    @Select("select * from `user` where id=#{userId} order by id DESC")
    User selectById(String userId);


    //通过用户角色筛选
    @Select("select * from `user` where role=#{role} order by id DESC")
    List<User> selectByRole(String role);

    //用户管理界面——通过用户名及用户角色筛选
    @Select("select * from `user` where role=#{role} and username=#{username} order by id DESC")
    List<User> selectByUsernameRole(String username, String role);

    //用户管理界面——通过用户名筛选
    @Select("select * from `user` where username=#{username} order by id DESC")
    List<User> selectByUsernameList(String username);

    //用户管理界面——通过用户ID更新用户信息
    @Update("update `user` set name = #{name}, phone = #{phone}, email = #{email}, role = #{role}" +
            "where id=#{id}")
    void updateUserByID(User user);


    //个人信息修改界面——通过用户名称修改个人信息
    @Update("update `user` set  password = #{password}, " +
            "name = #{name}, phone = #{phone}, email = #{email},  avatar = #{avatar}, role = #{role} " +
            "where username = #{username}")
    void updateUserByUsername(User user);

}
